package com.bing.bingPro.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.TTopologyDto;
import com.bing.bingPro.controller.vo.TNetworkTopologyUserVo;
import com.bing.bingPro.entity.TNetworkTopologyUser;
import com.bing.bingPro.mapper.TNetworkTopologyUserMapper;
import com.bing.bingPro.service.TNetworkTopologyUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author wangqilong
 * @description 针对表【t_network_topology_user】的数据库操作Service实现
 * @createDate
 */
@Service
public class TNetworkTopologyUserServiceImpl extends ServiceImpl<TNetworkTopologyUserMapper, TNetworkTopologyUser>
        implements TNetworkTopologyUserService {

    @Override
    public TNetworkTopologyUserVo getByUser(TTopologyDto tTopologyDto) {
        String userId = tTopologyDto.getUserId();
        if (isValidUserId(userId)){
            String encryptionUserId = MD5Lower(userId);
            return baseMapper.getOne(encryptionUserId);
        }
        return baseMapper.getOne(userId);
    }

    public static boolean isValidUserId(String userId) {
        // 判断长度不超过 32 位且不包含字母
        return userId.length() <= 32 && userId.matches("^[0-9]+$");
    }

    public static String MD5Lower(String plainText) {
        try {
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 使用指定的字节更新摘要
            md.update(plainText.getBytes());

            // digest()最后确定返回md5 hash值，返回值为8位字符串。md5 hash值是16位的hex值，实际是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值。1 固定值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String a = "15037321651";
        String s = MD5Lower(a);
        System.out.println(s);
    }
}




